import Ember from 'ember';
import Changeset from 'ember-changeset';
import tenantValidations from '../../../validations/tenant';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(tenantValidations, {
    saveFlag: 0,
    pathConfiger: Ember.inject.service("path-configer"),
    constants: Constants, //引入字典常量
    count: true,
    tenantModel: Ember.computed("tenantInfo", function() {
        var model = this.get("tenantInfo");
        if (!model) {
            return null;
        }
        return new Changeset(model, lookupValidator(tenantValidations), tenantValidations);
    }),
    mainController: Ember.inject.controller('business.mainpage'),
    editModel: null,
    uploadUrl: Ember.computed('property', function() {
        return this.get("pathConfiger").get("uploadUrl");
    }),
    deformation: function() {
        if ($(".cccc >div img").width() >= $(".cccc >div img").height()) {
            $(".cccc >div ").addClass("intergration_normal");
            $(".cccc >div img").css("height", "110px  ");
            $(".cccc >div img").css("width", "auto");
            console.log("this.widthcccccccccccccc==========", $(".cccc >div img").width());
            console.log('==========cccccccccccccccintergration_normal_height', $(".cccc >div img").height(), $(".cccc >div img").width());
        } else {
            $(".cccc >div ").addClass("intergration_normal");
            $(".cccc >div img").css("width", "110px ");
            $(".cccc >div img").css("height", "auto");
            console.log("this.heightccccc==========", $(".cccc >div img").height());
            console.log('==========cccccccccccccccintergration_normal', $(".cccc >div img").height(), $(".cccc >div img").width());
        }
    },
    actions: {
        invalid() {
            alert("invalid");
        },
        //编辑按钮
        detailEditClick: function() {
            this.set('detailEdit', true);
            this.set('tenantInfo.detailEdit', true);
        },
        //取消按钮
        detailCancel: function() {
            var id = this.get('id');
            var editMode = this.get('editMode');
            if (id && editMode != 'add') {
                this.set('detailEdit', false);
                this.get('tenantInfo').rollbackAttributes();
                var route = App.lookup('route:business.mainpage.tenant-detail');
                route.refresh(); //刷新页面
            } else {
                this.get('tenantInfo').rollbackAttributes();
                let mainpageController = App.lookup('controller:business.mainpage');
                mainpageController.switchMainPage('tenant-management');
            }
        },
        //保存
        saveTenant() {
            var _self = this;
            var editMode = this.get('editMode');
            var tenantModel = this.get('tenantModel');
            tenantModel.validate().then(function() {
                if (tenantModel.get('errors.length') === 0) {
                    App.lookup('controller:business.mainpage').openPopTip("正在保存");
                    /*if (_self.get('editMode') != 'add') {
                        _self.set("currentTenant", tenantModel);
                        _self.incrementProperty("saveFlag", 1);
                    }*/
                    //else{
                    tenantModel.save().then(function(tenant) { 
                        //手机号查重
                        App.lookup('controller:business.mainpage').showPopTip("保存成功");

                        if (_self.get('editMode') != 'add') {
                            _self.set("currentTenant", tenantModel);
                            _self.incrementProperty("saveFlag", 1);
                        }

                        if (editMode == 'add') {
                            var mainpageController = App.lookup('controller:business.mainpage');
                            mainpageController.switchMainPage('tenant-management');
                        } else {
                            _self.set('detailEdit', false);
                        }
                    });
                    //  }
                } else {
                    tenantModel.set("validFlag", Math.random());
                }
            });
        },
        //删除按钮
        delById: function() {
            var _self = this;
            App.lookup('controller:business.mainpage').showConfirm("是否确定删除此记录", function() {
                _self.cancelPassSubmit();
            });
        },

        //选择护理类型
        careTypeSelect(careType) {
            this.set('tenantInfo.careType', careType);
        },
        //选择周期
        periodSelect(period) {
            this.set('tenantInfo.period', period);
        },
        //选择类别
        countTypeSelect(countType) {
            this.set('tenantInfo.countType', countType);
            if (countType.get('typename') == '按时') {
                this.set('count', true);
            }
            if (countType.get('typename') == '按次') {
                this.set('count', false);
            }
        },
        //弹窗取消
        invitation() {
            this.set('showpopInvitePassModal', false);
        },
        //弹窗确定，删除记录
        cancelPassSubmit(tenant) {
            App.lookup('controller:business.mainpage').openPopTip("正在删除");
            this.set("showpopInvitePassModal", false);
            tenant.set("delStatus", 1);
            tenant.save().then(function() {
                App.lookup('controller:business.mainpage').showPopTip("删除成功");
                var mainpageController = App.lookup('controller:business.mainpage');
                mainpageController.switchMainPage('tenant-management');
            });
        },
    }
});
